package com.jiangnan.api.client

import com.jiangnan.api.service.AdminApiService
import com.jiangnan.api.service.AuthApiService
import com.jiangnan.api.service.BookApiService
import com.jiangnan.api.service.CommentApiService
import com.jiangnan.api.service.CreditApiService
import com.jiangnan.api.service.InteractionApiService
import com.jiangnan.api.service.PaymentApiService
import com.jiangnan.api.service.RecommendationApiService
import com.jiangnan.api.service.UserApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object BookApiClient {

    private const val BASE_URL = "https://mlhevrgnssub.sealoshzh.site"

    private val retrofit: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())  // JSON 转换器
            .build()
    }
    /**
     * 公用的 API 调用封装方法，将请求切换到 IO 线程
     */
    suspend fun <T> apiCall(call: suspend () -> T): T {
        return withContext(Dispatchers.IO) {
            call()
        }
    }

    // 构造 AuthApiService 实例
    val authApiService: AuthApiService by lazy {
        retrofit.create(AuthApiService::class.java)
    }

    val bookApiService: BookApiService by lazy {
        retrofit.create(BookApiService::class.java)
    }

    val userApiService : UserApiService by lazy {
        retrofit.create(UserApiService::class.java)
    }

    val adminApiService : AdminApiService by lazy {
        retrofit.create(AdminApiService::class.java)
    }

    val recommendationApiService : RecommendationApiService by lazy {
        retrofit.create(RecommendationApiService::class.java)
    }

    val interactionApiService : InteractionApiService by lazy {
        retrofit.create(InteractionApiService::class.java)
    }

    val credentialApiService : CreditApiService by lazy {
        retrofit.create(CreditApiService::class.java)
    }

    val paymentApiService : PaymentApiService by lazy {
        retrofit.create(PaymentApiService::class.java)
    }

    val commentApiService : CommentApiService by lazy {
        retrofit.create(CommentApiService::class.java)
    }



}